/*
 * Atomic Predicates for Transformers
 * 
 * Copyright (c) 2015 UNIVERSITY OF TEXAS AUSTIN. All rights reserved. Developed
 * by: HONGKUN YANG and SIMON S. LAM http://www.cs.utexas.edu/users/lam/NRL/
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * with the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimers.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimers in the documentation
 * and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the UNIVERSITY OF TEXAS AUSTIN nor the names of the
 * developers may be used to endorse or promote products derived from this
 * Software without specific prior written permission.
 * 
 * 4. Any report or paper describing results derived from using any part of this
 * Software must cite the following publication of the developers: Hongkun Yang
 * and Simon S. Lam, Scalable Verification of Networks With Packet Transformers
 * Using Atomic Predicates, IEEE/ACM Transactions on Networking, October 2017,
 * Volume 25, No. 5, pages 2900-2915 (first published as IEEE Early Access
 * Article, July 2017, Digital Object Identifier: 10.1109/TNET.2017.2720172).
 * 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
 * THE SOFTWARE.
 */
package transformer;

import java.util.*;

import common.PositionTuple;

public class StateUtility {
	
	HashMap<PositionTuple, Integer> num_leaves;
	HashMap<PositionTuple, Integer> num_nodes;
	HashMap<PositionTuple, Integer> heights;
	
	public StateUtility()
	{
		num_leaves = new HashMap<PositionTuple, Integer> ();
		heights = new HashMap<PositionTuple, Integer> ();
		num_nodes = new HashMap<PositionTuple, Integer> ();
	}
	
	public void reset()
	{
		num_leaves.clear();
		heights.clear();
	}
	
	public int get_height(AbstractState node)
	{
		if(node.nextState == null || node.nextState.isEmpty())
		{
			return 1;
		}else
		{
			int max_h = 0;
			for(AbstractState nn : node.nextState)
			{
				max_h = max(max_h, get_height(nn));
			}
			return max_h + 1;
		}
	}
	
	public int get_num_leaves(AbstractState node)
	{
		if(node.nextState == null || node.nextState.isEmpty())
		{
			return 1;
		}else
		{
			int num_l = 0;
			for(AbstractState nn : node.nextState)
			{
				num_l += get_num_leaves(nn);
			}
			return num_l;
		}
	}
	
	public int get_num_nodes(AbstractState node)
	{
	      if(node.nextState == null || node.nextState.isEmpty())
            {
                  return 1;
            }else
            {
                  int num_nodes = 1;
                  for(AbstractState nn : node.nextState)
                  {
                        num_nodes += get_num_leaves(nn);
                  }
                  return num_nodes;
            }
	}
	
	public int max(int x, int y)
	{
		if(x > y)
		{
			return x;
		}
		return y;
	}
	
	public void add_state(StateBDD s)
	{
		num_leaves.put(s.pt, get_num_leaves(s));
		heights.put(s.pt, get_height(s));
		num_nodes.put(s.pt, get_num_nodes(s));
	}
	
	public double get_ave_height()
	{
		return get_ave(heights.values());
	}
	
	public double get_ave_num_nodes()
	{
	      return get_ave(num_nodes.values());
	}
	
	public double[] get_order_height()
	{
		return get_order_stat(heights.values());
	}
	
	public double[] get_order_leaves()
	{
		return get_order_stat(num_leaves.values());
	}
	
	public double[] get_order_stat(Collection<Integer> c)
	{
		double [] order_stat = new double [3];
		
		ArrayList<Integer> nums = new ArrayList<Integer>(c);
		Collections.sort(nums);
		
		int len = nums.size();
		order_stat[0] = nums.get(0);
		order_stat[2] = nums.get(len-1);
		if(len %2 == 1)
		{
			order_stat[1] = nums.get(len/2);
		}else
		{
			order_stat[1] = (nums.get(len/2) + nums.get(len/2-1))/2;
		}
		
		return order_stat;
	}
	
	public double get_ave_num_leaves()
	{
		return get_ave(num_leaves.values());
	}
	
	public double get_ave(Collection<Integer> c)
	{
		double ave_num = 0.0;
		
		for(int n : c)
		{
			ave_num += n;
		}
		
		if(!c.isEmpty())
		{
			ave_num = ave_num / c.size();
		}
		
		return ave_num;
	}
	
	public void show(HashMap<PositionTuple, Integer> data)
	{
		for(PositionTuple pt : data.keySet())
		{
			System.out.println(pt + ":" + data.get(pt));
		}
	}
	
	public void show_heights()
	{
		show(heights);
	}
	
	public void show_num_leaves()
	{
		show(num_leaves);
	}

}
